package com.ruoyi.subject.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.subject.domain.QbSubjectItem;
import com.ruoyi.subject.mapper.QbSubjectItemMapper;
import com.ruoyi.subject.service.IQbSubjectItemService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 题项Service业务层处理
 *
 * @author lzhj
 * @date 2021-01-20
 */
@Service
public class QbSubjectItemServiceImpl implements IQbSubjectItemService {
    @Autowired
    private QbSubjectItemMapper qbSubjectItemMapper;

    /**
     * 查询题项
     *
     * @param id 题项ID
     * @return 题项
     */
    @Override
    public QbSubjectItem selectQbSubjectItemById(Long id) {
        return qbSubjectItemMapper.selectQbSubjectItemById(id);
    }

    /**
     * 查询题项列表
     *
     * @param qbSubjectItem 题项
     * @return 题项
     */
    @Override
    public List<QbSubjectItem> selectQbSubjectItemList(QbSubjectItem qbSubjectItem) {
        return qbSubjectItemMapper.selectQbSubjectItemList(qbSubjectItem);
    }

    /**
     * 新增题项
     *
     * @param qbSubjectItem 题项
     * @return 结果
     */
    @Override
    public int insertQbSubjectItem(QbSubjectItem qbSubjectItem) {
//        qbSubjectItemMapper.deleteQbSubjectItemBySubjectIds(new Long[]{qbSubjectItem.getSubjectId()});
        qbSubjectItem.setCreateTime(DateUtils.getNowDate());
        return qbSubjectItemMapper.insertQbSubjectItem(qbSubjectItem);
    }

    /**
     * 修改题项
     *
     * @param qbSubjectItem 题项
     * @return 结果
     */
    @Override
    public int updateQbSubjectItem(QbSubjectItem qbSubjectItem) {
        qbSubjectItem.setUpdateTime(DateUtils.getNowDate());
        return qbSubjectItemMapper.updateQbSubjectItem(qbSubjectItem);
    }

    /**
     * 批量删除题项
     *
     * @param ids 需要删除的题项ID
     * @return 结果
     */
    @Override
    public int deleteQbSubjectItemByIds(Long[] ids) {
        return qbSubjectItemMapper.deleteQbSubjectItemByIds(ids);
    }

    /**
     * 删除题项信息
     *
     * @param id 题项ID
     * @return 结果
     */
    @Override
    public int deleteQbSubjectItemById(Long id) {
        return qbSubjectItemMapper.deleteQbSubjectItemById(id);
    }

    @Override
    public void insertQbSubjectItemList(List<QbSubjectItem> qbSubjectItems) {
        if (CollectionUtils.isNotEmpty(qbSubjectItems)) {
            for (QbSubjectItem qbSubjectItem : qbSubjectItems) {
                Long id = qbSubjectItem.getId();

                if (StringUtils.isNotNull(id)) {
                    QbSubjectItem qbSubjectItem1 = qbSubjectItemMapper.selectQbSubjectItemById(qbSubjectItem.getId());
                    if (qbSubjectItem1 != null) {
                        qbSubjectItem.setUpdateTime(DateUtils.getNowDate());
                        qbSubjectItemMapper.updateQbSubjectItem(qbSubjectItem);
                    } else {
                        qbSubjectItem.setCreateTime(DateUtils.getNowDate());
                        qbSubjectItemMapper.insertQbSubjectItem(qbSubjectItem);
                    }
                } else {
                    qbSubjectItem.setCreateTime(DateUtils.getNowDate());
                    qbSubjectItemMapper.insertQbSubjectItem(qbSubjectItem);
                }
            }
        }
    }

    @Override
    public List<QbSubjectItem> selectQbSubjectItemBySubjectId(Long subjectId) {
        List<QbSubjectItem> subjectItemList = qbSubjectItemMapper.selectQbSubjectItemBySubjectId(subjectId);
        return subjectItemList;
    }

}
